package com.wngbms.websocket.notice;

import com.wngbms.websocket.entity.UserMetaInfo;
import feign.Feign;
import feign.Retryer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class FeignNoticeRemoteUser implements NoticeRemoteUser {

    private static final Map<String, ServiceFeign> serviceFeignClientMap = new ConcurrentHashMap<>();

    @Override
    public void noticeRemoteUser(UserMetaInfo userMetaInfo) {
        log.info("userMeta:{}", userMetaInfo);
        String serviceId = userMetaInfo.getServiceId();
        ServiceFeign serviceFeign = serviceFeignClientMap
                .computeIfAbsent(serviceId, args -> Feign.builder()
                        .retryer(new Retryer.Default(5000, 5000, 3))
                        .target(ServiceFeign.class, userMetaInfo.getAddress()));
        String resp = serviceFeign.forceCloseUser(userMetaInfo.getUserId());
        log.info("remote resp:{}",resp);
    }
}
